package com.pingan.haofang.searchcloud.batch.index;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import com.pingan.haofang.searchcloud.common.amqp.BaseMessageListener;
import com.pingan.haofang.searchcloud.index.dto.IndexBuildIncDTO;
import com.pingan.haofang.searchcloud.index.service.IndexBuildService;
import com.rabbitmq.client.Channel;

/**
 * 增量索引消息订阅
 *
 * @author LUYI374
 * @date 2017年3月24日
 * @since 1.0.0
 */
@Component
@ConditionalOnProperty("indexIncScheduleTask")
public class IndexIncMessageListener extends BaseMessageListener {

    static Logger LOG = LoggerFactory.getLogger(IndexIncMessageListener.class);

    private static final int RETRY_COUNT = 3;
    private static final String ENCODING = "UTF-8";

    @Autowired
    private IndexBuildService indexBuildService;

    public void onMessage(Message message, Channel channel) {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        handleMessage(IndexBuildIncDTO.class, message.getBody(), null, channel,
                deliveryTag, ENCODING, RETRY_COUNT, new MessageHandler<IndexBuildIncDTO>() {

                    @Override
                    public void doProcess(IndexBuildIncDTO message) {
                        indexBuildService.mergeOrExecuteIndexInc(message);
                    }
                });
    }

}
